class ElementCreator {
    static removePrompt(type, prompt) {
        let textarea = null;
        if (type === 'positive') {
            textarea = document.querySelector(ElementSelector.sys_txt2img_prompt_positive_selector);
        } else {
            textarea = document.querySelector(ElementSelector.sys_txt2img_prompt_negative_selector);
        }
        const promptValue = textarea.value.trim();
        // 找到匹配位置
        const index = promptValue.indexOf(prompt);
        // 保存结果
        let result;
        // 判断前一个位置是否为 “,” 如果是，则去除
        if (promptValue.charAt(index - 1) === ',') {
            result = promptValue.replace(`,${prompt}`, '');
        } else if (promptValue.charAt(index + prompt.length)) {
            result = promptValue.replace(`${prompt},`, '');
        } else {
            result = promptValue.replace(prompt, '');
        }
        textarea.value = result;
        PublicUtils.updateInputElement(textarea);
    }

    static createButton(buttonText, {onClick, onRightClick}, ...classList) {
        const button = document.createElement("button");
        button.classList.add("svelte-1ipelgc", "secondary", "gradio-button", "sm");
        button.classList.add(...classList)
        button.textContent = buttonText;
        button.addEventListener('click', onClick);
        button.addEventListener('contextmenu', onRightClick);
        return button;
    }

    static createSelectedPromptContainer(type = "positive") {
        const container = document.createElement("div");
        container.classList.add(PublicUtils.formateSelector(ElementSelector.zj_prompt_selected_selector));
        const titleContainer = document.createElement("div");
        const tagContainer = document.createElement("div");
        let promptContainer = null;
        if (type === 'positive') {
            container.id = PublicUtils.formateSelector(ElementSelector.zj_prompt_selected_positive_selector);
            titleContainer.textContent = "已选正向提示词";
            promptContainer = document.querySelector(ElementSelector.sys_txt2img_prompt_positive_container_selector);
        } else {
            container.id = PublicUtils.formateSelector(ElementSelector.zj_prompt_selected_negative_selector);
            titleContainer.textContent = "已选反向提示词";
            promptContainer = document.querySelector(ElementSelector.sys_txt2img_prompt_negative_container_selector);
        }
        container.appendChild(titleContainer);
        container.appendChild(tagContainer);
        promptContainer.insertAdjacentElement("afterend", container);
    }

    static createSelectedPrompt(type = "positive", promptName, prompt) {
        let container = null;
        if (type === 'positive') {
            container = document.querySelector(ElementSelector.zj_prompt_selected_positive_selector);
        } else {
            container = document.querySelector(ElementSelector.zj_prompt_selected_negative_selector);
        }

        const el = document.createElement("div");
        el.classList.add("zj-prompt-selected-tag");

        const selectedTagName = document.createElement("div");
        selectedTagName.textContent = promptName;

        const deleteButton = this.createButton("×", {
            onClick: e => {
                try {
                    this.removePrompt(type, prompt);
                } catch (e) {

                }
                container.lastElementChild.removeChild(el);
                container.style.height = container.scrollHeight + 'px';
            }
        })
        deleteButton.style.padding = '0';
        deleteButton.style.margin = '0';

        el.appendChild(selectedTagName);
        el.appendChild(deleteButton);
        container.lastElementChild.appendChild(el);
    }
}